package com.example.demo.controller;

import com.example.demo.config.error.BizException;
import com.example.demo.controller.dto.PatientUpdateDTO;
import com.example.demo.controller.vo.DoctorInfoVO;
import com.example.demo.dao.DoctorDao;
import com.example.demo.dao.PatientDao;
import com.example.demo.entity.Doctor;
import com.example.demo.entity.OpenIdJson;
import com.example.demo.entity.Patient;
import com.example.demo.utils.HttpUtil;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author: WinstonDeng
 * @Description:
 * @Date: Created in 9:09 2020/4/8
 * @Modified by:
 */
@RestController
public class PatientController {
    private String appID = "";
    private String appSecret = "";

    @Autowired
    private PatientDao patientDao;

    @Autowired
    private DoctorDao doctorDao;

    /**
     * 暂不使用
     * @param code
     * @return
     * @throws IOException
     */
    @PostMapping("/user/login")
    public String userLogin(@RequestParam("code") String code) throws IOException {
        String result = "";
        try{//请求微信服务器，用code换取openid。HttpUtil是工具类，后面会给出实现，Configure类是小程序配置信息，后面会给出代码
            result = HttpUtil.doGet(
                    "https://api.weixin.qq.com/sns/jscode2session?appid="
                            + this.appID + "&secret="
                            + this.appSecret + "&js_code="
                            + code
                            + "&grant_type=authorization_code", null);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        ObjectMapper mapper = new ObjectMapper();
        OpenIdJson openIdJson = mapper.readValue(result,OpenIdJson.class);
        System.out.println(result.toString());
        System.out.println(openIdJson.getOpenid());
        return result;
    }

    @PostMapping("/patient/login")
    @ResponseBody
    public ResponseEntity<Result> patientLogin(@RequestParam("openid") String openId, @RequestParam("nickName") String nickName,
                                               @RequestParam("gender") Integer gender, @RequestParam(name="role", defaultValue ="1") int role) throws BizException {
        if(openId == null || openId.isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(412,"openid不能为空"));
        }else {
            if(role==1){
                //patient
                return ResponseEntity.ok(ResultUtil.success(patientDao.getByOpenIdFirst(openId, nickName, gender)));
            }else{
                //doctor
                Doctor doctor = doctorDao.getByOpenId(openId);
                if(doctor == null){
                    return ResponseEntity.ok(ResultUtil.error(201,"未绑定微信"));
                }
                return ResponseEntity.ok(ResultUtil.success(new DoctorInfoVO(doctor)));
            }

        }
    }

    @PostMapping("/patient/update")
    @ResponseBody
    public ResponseEntity<Result> patientUpdate(@RequestBody PatientUpdateDTO patientUpdateDTO) throws BizException{
        if(patientUpdateDTO.getOpenid() == null || patientUpdateDTO.getOpenid().isEmpty()){
            return ResponseEntity.ok(ResultUtil.error(412,"openid不能为空"));
        }else {
            Patient patient = patientDao.getByOpenId(patientUpdateDTO.getOpenid());
            if (patient == null){
                return ResponseEntity.ok(ResultUtil.error(404,"patient未找到"));
            }else {
                patient.setRealName(patientUpdateDTO.getRealName());
                patient.setAge(patientUpdateDTO.getAge());
                patient.setIdCard(patientUpdateDTO.getIdCard());
                patient.setPhoneNum(patientUpdateDTO.getPhoneNum());
                patient.setGender(patientUpdateDTO.getGender());
                return ResponseEntity.ok(ResultUtil.success(patientDao.updatePatient(patient)));
            }
        }
    }
}
